<template>
  <el-dialog
    title="编辑"
    :model-value="props.editVisible"
    width="30%"
    :before-close="close"
    append-to-body
  >
    <el-form ref="formref" :model="form">
      <el-form-item label="姓名" label-width="80px" prop="userName">
        <el-input v-model="form.userName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="账号" label-width="80px" prop="account">
        <el-input v-model="form.account" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleEditSubmit">保存</el-button>
        <el-button @click="handleEditClose">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="Edit">
const { proxy } = getCurrentInstance();
const emit = defineEmits(['handleEditClose', 'handleEditSubmit']);

// 父组件传递参数
const props = defineProps({
  editVisible: {
    type: Boolean
  },
  listItemInfo: {
    type: Object
  }
});

const form = ref({
  userName: '',
  account: ''
})

// 取消
function handleEditClose() {
  emit('handleEditClose', false)
  proxy.resetForm("formref");
}

// 提交
function handleEditSubmit() {
  emit('handleEditSubmit', form.value)
  proxy.resetForm("formref");
}

// 关闭
function close() {
  emit('handleEditClose', false)
  proxy.resetForm("formref");
}

// 回显当前信息
watch(() => props.listItemInfo, listItemInfo => {
  if (listItemInfo) {
    form.value = { userName: listItemInfo.userName, account: listItemInfo.account };
  }
},{ immediate: true });
</script>

<style scoped>

</style>
